package com.xc.ezplan.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.xc.ezplan.admin.entity.AdminRole;
import com.xc.ezplan.admin.service.AdminRoleService;
import com.xc.ezplan.admin.vo.AdminRoleVo;
import com.xc.ezplan.core.base.BaseSearchBody;
import com.xc.ezplan.core.base.EzResult;
import com.xc.ezplan.core.exception.BadRequestException;
import com.xc.ezplan.log.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * @author lixingchen
 * @date 2022/11/19
 * @description 角色管理-后台角色
 */
@RestController
@RequestMapping("/role")
@Api(tags = "后台角色")
@RequiredArgsConstructor
public class AdminRoleController {

	private final AdminRoleService adminRoleService;

	private static final String ENTITY_NAME = "role";

	@Log("新增角色")
	@ApiOperation("新增角色")
	@PostMapping
	@SaCheckPermission("role.admin.add")
	public EzResult<Object> createRole(@Validated @RequestBody AdminRoleVo adminRoleVo) {
		if (adminRoleVo.getId() != null) {
			throw new BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID");
		}
		adminRoleService.createRole(adminRoleVo);
		return EzResult.ok();
	}

	@Log("删除角色")
	@ApiOperation("删除角色")
	@DeleteMapping
	@SaCheckPermission("role.admin.delete")
	public EzResult<Object> deleteRole(@RequestBody List<Long> ids) {
		if (ids.contains(1)) {
			return EzResult.failed("不能删除管理员角色");
		}
		adminRoleService.deleteRole(ids);
		return EzResult.ok();
	}

	@Log("修改角色")
	@ApiOperation("修改角色")
	@PutMapping
	@SaCheckPermission("role.admin.update")
	public EzResult<Object> updateRole(@Validated @RequestBody AdminRoleVo adminRoleVo) {
		adminRoleService.updateRole(adminRoleVo);
		return EzResult.ok();
	}

	@ApiOperation("查询角色")
	@GetMapping
	public EzResult<Object> queryRole(@RequestParam(required = false) Long id) {
		return EzResult.ok(adminRoleService.findRoleById(id));
	}

	@ApiOperation("按条件分页查询角色")
	@PostMapping("/page")
	public EzResult<Object> pageQueryRole(@RequestBody BaseSearchBody<AdminRole> baseSearchBody) {
		return EzResult
				.ok(adminRoleService.pageQueryRole(baseSearchBody.getSearchCondition(), baseSearchBody.getPage()));
	}

	@ApiOperation("返回全部的角色")
	@GetMapping(value = "/all")
	public EzResult<Object> queryAllRole() {
		return EzResult.ok(adminRoleService.queryAllRole());
	}

	@ApiOperation("获取用户级别")
	@GetMapping(value = "/level")
	public EzResult<Object> getRoleLevel() {
		return EzResult.ok(adminRoleService.getRoleLevel());
	}

	@Log("修改角色菜单")
	@ApiOperation("修改角色菜单")
	@PutMapping(value = "/menu")
	@SaCheckPermission("role.admin.config")
	public EzResult<Object> updateRoleMenu(@RequestBody AdminRoleVo adminRoleVo) {
		adminRoleService.updateRoleMenu(adminRoleVo);
		return EzResult.ok();
	}

	@ApiOperation("导出角色数据")
	@GetMapping(value = "/download")
	public void exportRole(List<AdminRoleVo> adminRoleVos, HttpServletResponse response) throws IOException {
		adminRoleService.exportRole(adminRoleVos, response);
	}
}
